package de.justphil.tcg.tcgserver.websockets.adapters;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import de.justphil.tcg.tcgserver.commons.domain.Card;
import de.justphil.tcg.tcgserver.commons.domain.CardInstance;
import de.justphil.tcg.tcgserver.commons.domain.Property;
import de.justphil.tcg.tcgserver.commons.domain.collections.CardInstanceCollection;
import de.justphil.tcg.tcgserver.commons.config.GameConfig;
import de.justphil.tcg.tcgserver.game.mechanics.monster.Monster;
import de.justphil.tcg.tcgserver.game.mechanics.monster.MonsterList;

public class DeckAdapter {

	public MonsterList[] adaptDecks(CardInstanceCollection[] decks, Map<Long, Card> cardMap) {
		MonsterList[] newDecks = new MonsterList[GameConfig.NUMBER_OF_PLAYERS_PER_GAME];
		
		CardInstanceCollection cardInstances		= null;
		MonsterList monsterList				= null;
		Card c								= null;
		Monster m							= null;
		
		for(int i = 0; i < GameConfig.NUMBER_OF_PLAYERS_PER_GAME; i++) {
			cardInstances = decks[i];
			monsterList = new MonsterList();
			
			for (CardInstance ci : cardInstances.getCollection()) {
				c = cardMap.get( ci.getCardId() );
				m = new Monster(ci.getId(), c.getLife(), c.getAttack(), c.getDefense(), c.getAgility(), getPropertyTypesArray(c.getProperties()));
				monsterList.add(m);
			}
			
			newDecks[i] = monsterList;
		}
		
		return newDecks;
	}
	
	public Map<Long, Long> createCardInstanceIdToCardIdMapping(CardInstanceCollection[] decks) {
		Map<Long, Long> ciIdToCardIdMapping = new HashMap<Long, Long>();
		
		for (int i = 0; i < decks.length; i++) {
			for (CardInstance ci : decks[i].getCollection()) {
				ciIdToCardIdMapping.put(ci.getId(), ci.getCardId());
			}
		}
		
		return ciIdToCardIdMapping;
	}
	
	private int[] getPropertyTypesArray(Set<Property> properties) {
		int[] out = new int[properties.size()];
		
		int i = 0;
		for (Property p : properties) {
			out[i] = (int) p.getId();
			
			i++;
		}
		
		return out;
	}
	
}
